using System.Collections;
using UnityEngine;

public class DistrictDetector : MonoBehaviour
{
	public static DistrictDetector instance;

	public DistrictInfo[] districts;

	public string currentDistrictName = string.Empty;

	private Vector3 playerPosition = Vector3.zero;

	private void Awake()
	{
		instance = this;
	}

	private void Start()
	{
	}

	private IEnumerator DetectorCorutine()
	{
		while (true)
		{
			yield return new WaitForSeconds(1f);
			string newDistrictName = GetCurrentDistrictName();
			if (!string.IsNullOrEmpty(newDistrictName) && currentDistrictName != newDistrictName)
			{
				currentDistrictName = newDistrictName;
				OnEnterNewDistrict();
			}
		}
	}

	private void OnEnterNewDistrict()
	{
	}

	private string GetCurrentDistrictName()
	{
		if ((bool)GameController.instance.playerScript)
		{
			playerPosition = GameController.instance.playerScript.transform.position;
		}
		DistrictInfo[] array = districts;
		for (int i = 0; i < array.Length; i++)
		{
			DistrictInfo districtInfo = array[i];
			if (Contains(playerPosition.x, districtInfo.xStart, districtInfo.xEnd) && Contains(playerPosition.z, districtInfo.zStart, districtInfo.zEnd))
			{
				return districtInfo.districtName;
			}
		}
		return string.Empty;
	}

	public string GetCurrentDistrictName(Vector3 pos)
	{
		DistrictInfo[] array = districts;
		for (int i = 0; i < array.Length; i++)
		{
			DistrictInfo districtInfo = array[i];
			if (Contains(pos.x, districtInfo.xStart, districtInfo.xEnd) && Contains(pos.z, districtInfo.zStart, districtInfo.zEnd))
			{
				return districtInfo.districtName;
			}
		}
		return string.Empty;
	}

	private bool Contains(float value, float a, float b)
	{
		return (value >= a && value <= b) || (value >= b && value <= a);
	}
}
